package com.yuncommunity.cloudlab

import com.yuncommunity.cloudlab.model.User
import com.yuncommunity.cloudlab.repository.UserRepository
import com.yuncommunity.cloudlab.storage.FileStorage
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.util.DigestUtils

@SpringBootApplication
class CloudLabApplication {
    @Autowired
    lateinit var fileStorage: FileStorage

    @Autowired
    lateinit var userRepository: UserRepository

    @Bean
    fun run() = CommandLineRunner {
        fileStorage.init()

        var u: User? = userRepository.findByName("admin")
        if (u == null) {
            u = User()
            u.name = "admin"
            u.userType = "系统管理员"
            u.password = DigestUtils.md5DigestAsHex("admin".toByteArray())
            userRepository.save(u)
        }
    }
}

fun main(args: Array<String>) {
    runApplication<CloudLabApplication>(*args)
}
